import 'dart:math';

import 'package:fl_chart/fl_chart.dart';

class Biorhythm {
  // 计算从出生日期到目标日期的天数
  static int calculateDays(DateTime birthDate, DateTime targetDate) {
    return targetDate.difference(birthDate).inDays;
  }

  // 计算节律周期
  static Map<String, Map<String, dynamic>> calculateBioRhythms(DateTime birthDate, DateTime targetDate) {
    int totalDays = calculateDays(birthDate, targetDate);

    // 生物节律周期分别为23、28、33
    int physicalCycle = totalDays % 23;  // 体力周期
    int emotionalCycle = totalDays % 28; // 情绪周期
    int intellectualCycle = totalDays % 33; // 智力周期

    // 计算节律阶段的百分比
    double physicalPercentage = _getPercentage(physicalCycle, 23);
    double emotionalPercentage = _getPercentage(emotionalCycle, 28);
    double intellectualPercentage = _getPercentage(intellectualCycle, 33);

    // 计算下一个顶峰距离当前的天数
    int physicalNextPeak = _getNextPeakDays(physicalCycle, 23);
    int emotionalNextPeak = _getNextPeakDays(emotionalCycle, 28);
    int intellectualNextPeak = _getNextPeakDays(intellectualCycle, 33);

    // 判断value的类别
    int physicalCategory = _getValueCategory(physicalPercentage);
    int emotionalCategory = _getValueCategory(emotionalPercentage);
    int intellectualCategory = _getValueCategory(intellectualPercentage);

    return {
      'Physical': {
        'value': double.parse(physicalPercentage.toStringAsFixed(2)),
        'day': physicalNextPeak,
        'category': physicalCategory
      },
      'Emotional': {
        'value': double.parse(emotionalPercentage.toStringAsFixed(2)),
        'day': emotionalNextPeak,
        'category': emotionalCategory
      },
      'Intellectual': {
        'value': double.parse(intellectualPercentage.toStringAsFixed(2)),
        'day': intellectualNextPeak,
        'category': intellectualCategory
      },
      'average': {
        'value': double.parse(((physicalPercentage+emotionalPercentage+intellectualPercentage)/3).toString()).toInt()
      }
    };
  }

  // 根据余数计算百分比
  static double _getPercentage(int cycleValue, int cycleLength) {
    return (cycleValue / cycleLength) * 100;
  }

  // 计算距离下一个顶峰的天数
  static int _getNextPeakDays(int cycleValue, int cycleLength) {
    // 顶峰是周期的开始和结束，分别对应0和周期长度。
    int remainingDays = cycleLength - cycleValue;
    return remainingDays;
  }

  // 判断value的类别
  static int _getValueCategory(double value) {
    if (value <= 20) {
      return 1;
    } else if (value <= 40) {
      return 2;
    } else if (value <= 60) {
      return 3;
    } else if (value <= 80) {
      return 4;
    } else {
      return 5;
    }
  }

  // 生成未来30天的节律数据
  static List<List<FlSpot>> generateFuture30DaysData(DateTime targetDate) {
    DateTime birthDate = DateTime(1990, 1, 1);  // 假设出生日期为1990年1月1日
    List<List<FlSpot>> result = [
      [], // Physical
      [], // Emotional
      [], // Intellectual
    ];

    // 往后30天
    for (int i = 0; i < 30; i++) {
      DateTime currentDate = targetDate.add(Duration(days: i));
      int totalDays = calculateDays(birthDate, currentDate);

      // 生物节律周期分别为23、28、33
      int physicalCycle = totalDays % 23;  // 体力周期
      int emotionalCycle = totalDays % 28; // 情绪周期
      int intellectualCycle = totalDays % 33; // 智力周期

      // 计算节律阶段的百分比
      double physicalPercentage = double.parse(_getPercentage(physicalCycle, 23).toStringAsFixed(2));
      double emotionalPercentage = double.parse(_getPercentage(emotionalCycle, 28).toStringAsFixed(2));
      double intellectualPercentage = double.parse(_getPercentage(intellectualCycle, 33).toStringAsFixed(2));

      // 将结果添加到各自的节律列表中
      result[0].add(FlSpot(i.toDouble(), physicalPercentage));  // Physical
      result[1].add(FlSpot(i.toDouble(), emotionalPercentage)); // Emotional
      result[2].add(FlSpot(i.toDouble(), intellectualPercentage)); // Intellectual
    }

    return result;
  }
}